package com.example.littlepainter.home

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.navigation.findNavController
import androidx.recyclerview.widget.RecyclerView
import com.example.littlepainter.db.Picture
import com.example.littlepainter.databinding.LayoutPictureItemBinding

class PictureAdapter: RecyclerView.Adapter<PictureAdapter.MyViewHolder>() {
    private var mPictures = emptyList<Picture>()

    override fun getItemCount(): Int {
        return mPictures.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        val binding = LayoutPictureItemBinding.inflate(inflater,parent,false)
        return MyViewHolder(binding)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.bind(mPictures[position])
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setData(newData: List<Picture>){
        mPictures = newData
        notifyDataSetChanged()
    }

    //定义ViewHolder
    class MyViewHolder(private val binding:LayoutPictureItemBinding):RecyclerView.ViewHolder(binding.root){
        fun bind(pictureModel: Picture){
            binding.imageView.setImageBitmap(pictureModel.thumbnail)
            binding.root.setOnClickListener {
                //切换到绘制界面
                val action = HomeFragmentDirections.actionHomeFragmentToDrawFragment(pictureModel)
                binding.root.findNavController().navigate(action)
            }
        }
    }
}